//
//  WDPDContentCell.m
//  Zhihuijinfu
//
//  Created by 天Ming是个小可爱 on 2017/1/4.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDPDContentCell.h"
#import "WDProductDynamic.h"
@interface WDPDContentCell ()
@property (weak, nonatomic) IBOutlet UIImageView *topLine;
@property (weak, nonatomic) IBOutlet UIImageView *statueImage;

@property (weak, nonatomic) IBOutlet UIImageView *bottomLine;
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *descrp;
@property (weak, nonatomic) IBOutlet UIView *lineBlack;
@property (weak, nonatomic) IBOutlet UIButton *contLabel;

@end

@implementation WDPDContentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // 处理图片的拉伸
    UIImage *norImage = [UIImage imageNamed:@"icon_chatBox"];
    CGFloat w = norImage.size.width*0.9;
    CGFloat h = norImage.size.height*0.9;
    UIImage *newImage = [norImage resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w) resizingMode:UIImageResizingModeStretch];
    
    self.contLabel.userInteractionEnabled = NO;
    
    [self.contLabel setBackgroundImage:newImage forState:UIControlStateNormal];
    
    self.iconImage.layer.masksToBounds = YES;
    self.iconImage.layer.cornerRadius = 22;
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setDataWithModel:(WDProductDynamic *)dataModel index:(NSInteger)index maxIndex:(NSInteger)maxIndex
{
    
    self.descrp.text = dataModel.product_des;
    
    self.name.text = dataModel.title;
    
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:dataModel.image_url] placeholderImage:[UIImage imageNamed:@"defaultIcon"]];
    
    if (![dataModel.product_html isEqualToString:@""] && dataModel.product_html) {
        
        self.contLabel.hidden = NO;
        
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithData:[dataModel.product_html dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        [self.contLabel setAttributedTitle:attrStr forState:UIControlStateNormal];
        
        self.contLabel.titleLabel.numberOfLines = 0;
        
    }else
    {
        self.contLabel.hidden = YES;
    }
    
    
    if (index == 0 && index != maxIndex) {   // 第一个
        self.topLine.hidden = YES;
        self.statueImage.image = [UIImage imageNamed:@"android -xhdp"];
        self.timeLabel.hidden = NO;
        self.bottomLine.hidden = NO;
        self.timeLabel.text = dataModel.create_time;
        self.lineBlack.hidden = NO;
        self.descrp.textColor = WDNavSeleColor;
    } else if (index == 0 && index == maxIndex) {
        
        self.topLine.hidden = YES;
        self.statueImage.image = [UIImage imageNamed:@"android -xhdp"];
        self.timeLabel.hidden = NO;
        self.bottomLine.hidden = YES;
        self.timeLabel.text = dataModel.create_time;
        self.lineBlack.hidden = YES;
        self.descrp.textColor = WDNavSeleColor;
        
    } else if (index!= 0 && index == maxIndex) // 最后一个
    {
        self.topLine.hidden = NO;
        self.statueImage.image = [UIImage imageNamed:@"android -xhdpi copy "];
        self.timeLabel.hidden = NO;
        self.bottomLine.hidden = YES;
        self.timeLabel.text = dataModel.create_time;
        self.lineBlack.hidden = YES;
        self.descrp.textColor = ColorForRGB(0x4c4c4c);
    }else  // 中间内容
    {
        self.topLine.hidden = NO;
        self.statueImage.image = [UIImage imageNamed:@"android -xhdpi copy "];
        self.timeLabel.hidden = NO;
        self.bottomLine.hidden = NO;
        self.timeLabel.text = dataModel.create_time;
        self.lineBlack.hidden = NO;
        self.descrp.textColor = ColorForRGB(0x4c4c4c);
    }
}

- (void)setProductDynamic:(WDProductDynamic *)productDynamic
{
    _productDynamic = productDynamic;
    
    [self layoutIfNeeded];
    
    [self.contLabel layoutIfNeeded];
    
    if (!self.contLabel.hidden) {
    
        self.contLabel.contentEdgeInsets = UIEdgeInsetsMake(20, 30, 10, 20);
        
        [self.contLabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        
        productDynamic.cellH = CGRectGetMaxY(self.contLabel.frame) + 40;
        
    } else {
        
        productDynamic.cellH = CGRectGetMaxY(self.iconImage.frame) + 20;
        
    }
    
}

@end
